package com.ruoyi.project.dict.hanyucizu.service.impl;

import com.ruoyi.common.utils.CizuUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.cizunew.domain.Cizu;
import com.ruoyi.project.dict.cizunew.service.ICizuService;
import com.ruoyi.project.dict.hanyucizu.domain.HanyuCizu;
import com.ruoyi.project.dict.hanyucizu.mapper.HanyuCizuMapper;
import com.ruoyi.project.dict.hanyucizu.service.IHanyuCizuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 百度词组Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-01-29
 */
@Service
@DataSource(DataSourceType.DICT)
public class HanyuCizuServiceImpl implements IHanyuCizuService {

    @Autowired
    private HanyuCizuMapper hanyuCizuMapper;

    @Autowired
    private ICizuService cizuService;

    /**
     * 查询百度词组
     *
     * @param id 百度词组ID
     * @return 百度词组
     */
    @Override
    public HanyuCizu selectHanyuCizuById(Long id) {
        return hanyuCizuMapper.selectHanyuCizuById(id);
    }

    /**
     * 查询百度词组列表
     *
     * @param hanyuCizu 百度词组
     * @return 百度词组
     */
    @Override
    public List<HanyuCizu> selectHanyuCizuList(HanyuCizu hanyuCizu) {
        return hanyuCizuMapper.selectHanyuCizuList(hanyuCizu);
    }

    /**
     * 新增百度词组
     *
     * @param hanyuCizu 百度词组
     * @return 结果
     */
    @Override
    public int insertHanyuCizu(HanyuCizu hanyuCizu) {
        return hanyuCizuMapper.insertHanyuCizu(hanyuCizu);
    }

    /**
     * 修改百度词组
     *
     * @param hanyuCizu 百度词组
     * @return 结果
     */
    @Override
    public int updateHanyuCizu(HanyuCizu hanyuCizu) {
        return hanyuCizuMapper.updateHanyuCizu(hanyuCizu);
    }

    /**
     * 删除百度词组对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHanyuCizuByIds(String ids) {
        return hanyuCizuMapper.deleteHanyuCizuByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除百度词组信息
     *
     * @param id 百度词组ID
     * @return 结果
     */
    @Override
    public int deleteHanyuCizuById(Long id) {
        return hanyuCizuMapper.deleteHanyuCizuById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int impirt2cizu(Long id) {

        HanyuCizu hanyuCizu = hanyuCizuMapper.selectHanyuCizuById(id);
        Cizu query = new Cizu();
        query.setCi(hanyuCizu.getCi());
        List<Cizu> cizus = cizuService.selectCizuList(query);
        if (cizus.size() <= 0) {
            return 0;
        }
        Cizu cizu = cizus.get(0);
        boolean update = false;
        if (StringUtils.isEmpty(cizu.getPinyin()) && StringUtils.isNotEmpty(hanyuCizu.getPinyin())) {
            cizu.setPinyin(hanyuCizu.getPinyin());
            update = true;
        }
        if (StringUtils.isEmpty(cizu.getCyjs()) && StringUtils.isNotEmpty(hanyuCizu.getBaike())) {
            cizu.setCyjs(CizuUtil.format(hanyuCizu.getBaike()));
            //update = true;
        }

        if (update) {
            cizuService.updateCizu(cizu);
        }

        hanyuCizu.setStatus(1);
        return hanyuCizuMapper.updateHanyuCizu(hanyuCizu);

    }
}
